Because client-side business logic is required to create a highly interactive user interface and because duplication of business logic is inefficient and error-prone, you should try to avoid using database integrity-checking features. Sometimes, however, it's unavoidable. You usually use database integrity checking when users can access a database in many ways (using Enterprise Objects Framework applications, non-Enterprise Objects Framework applications, and interactive SQL sessions, for example). In this case, you may have to use the features of your database server to assure your data's integrity. As a result, you may choose to implement integrity checking in both your Enterprise Objects Framework applications and in the database.
The following sections discuss guidelines for using the
integrity-checking features of your database in concert with an Enterprise Objects Framework application.
If you define defaults in your database, you should specify the defaults in your Enterprise Objects Framework application as well. Generally, you assign default values in your enterprise object's awakeFromInsertion method (awakeFromInsertionInEditingContext: in Objective-C). For example:
public void awakeFromInsertion(EOEditingContext ec)In Objective-C:
{
super.awakeFromInsertion(ec);
// Assign current date to memberSince
if (memberSince == null)
memberSince = new NSGregorianDate();
}
- (void)An alternative is to fetch newly inserted objects immediately after you save them to the database. If you don't assign the default values before you save an object and you don't refetch the object from the database after you save, the Framework's object snapshots will not be in sync with the contents of the database. As a result, the Framework may prevent subsequent updates to the object.
awakeFromInsertionInEditingContext:(EOEditingContext *)ec
{
[super awakeFromInsertionInEditingContext:ec];
// Assign current date to memberSince
if (!memberSince)
memberSince = [[NSCalendarDate date] retain];
}
You should implement data validation in your Enterprise Objects Framework application whether or not you use database rules. Depending on the nature of the validation, use a formatter or implement an appropriate validate... method in your enterprise object class. For more information, see the chapter "Designing Enterprise Objects".
If you use database triggers and constraints, you will have to duplicate the logic in your Enterprise Objects Framework application. In some cases, the duplication won't hurt anything, but in other cases you have to provide special handling to avoid run-time errors.
For example, suppose you have a constraint specifying that you can't delete a department if it still has employees. In addition, you specify the Deny delete rule on the Department entity's employees relationship. When a user attempts to delete a department, Enterprise Objects Framework verifies that the corresponding Department object has no employees. If the department has one or more employees, the Framework doesn't allow the delete.
Further suppose that a user moves all the employees from one department to another, deletes the empty department, then saves all changes. Enterprise Objects Framework analyzes the object graph to determine what operations have taken place. It orders the operations by analyzing the relationships and identifying "master" and "detail" entities. In this example, the Department object, the master, would not be deleted until all the employees are updated to reflect their new department. In most cases, Enterprise Objects Framework just does the "right thing." However, if you discover a sequencing problem with your application, you can customize the order in which database operations are performed. For a complete description of the Framework's default ordering algorithm and how to programmatically reorder operations, see the section "How Do I Order Database Operations?".
Table of Contents
Next Section